<script setup lang="ts">
  import { requestUploadFile } from '@/service'
  import { useChannelStore } from '@/store/channel'
  import { useChatStore } from '@/store/chat.ts'
  import { useFileDialog } from '@vueuse/core'
  import { useSessionStore } from '@/store/session'
  import { find } from 'lodash'
  import socketServer from '@/plugins/socketServer'
  import { evaluateState, FAction } from '@/plugins/constants.ts'
  import { v7 as uuidv7 } from "uuid";
  import { showToast } from "vant";
  const channel = useChannelStore()
  const chat = useChatStore()
  import { computed } from 'vue'
  import sendMessage from "@/plugins/message.ts";
  import session from "@/plugins/session.ts";
  const visible = defineModel('visible')
  const { open, onChange } = useFileDialog()
  // 评价按钮是否显示
  const showEvaluation = computed(() => {
    const session = useSessionStore()
    return channel.SystemInfo.evaluateAction && (session.FAction === FAction.ROBOT || session.FAction === FAction.AGENT)
  })
  // 结束会话按钮是否展示
  const showEndSession = computed(() => {
    const session = useSessionStore()
    return (channel.SystemInfo.endSessionAction && session.FAction === FAction.AGENT)
  })
  const openFileDialog = () => {
    open({
      multiple: false,
      accept: channel.uploadType,
      reset: true,
    })
  }
  onChange(async (files: any) => {
    const parts = files[0].name.split('.')
    const suffix = parts[parts.length - 1]
    const limits = find(channel.uploadInfo, (item) => {
      return item.fileType.includes('.' + suffix)
    })
    const maxSize = limits.maxFileSize * 1024 * 1024
    if (files[0].size > maxSize) {
      alert('文件大小超过限制')
      return
    }
    const params = {
      module: 'fsCenter_onLine',
      scene: limits.scene || 'fsCenter_onLine_appSendFile',
      upfile: files[0],
    }
    // 上传文件
    const successFile = await requestUploadFile(params)
    console.log('successFile', successFile)
  })
  // 主动发起评价
  const sendEvaluation = () => {
    const session = useSessionStore()
    // 发送评价
    switch (session.FAction) {
      case FAction.ROBOT:
      case FAction.DESTROY_ROBOT:
        handleRobotEvaluation()
            break
      case FAction.AGENT:
        handleAgentEvaluation()
            break

    }
  }
  // 机器人评价业务
  const handleRobotEvaluation = () => {
    const session = useSessionStore()
    // 判断机器人评价是否已经完成
    if (session.robotEvaluate === evaluateState.complete) {
      showToast('您已评价,请勿重复评价！')
      return
    }
    // 判断当前是否已经有评价表单
    const evalForm = find(chat.messageList, (item:any) => {
      return item.target === 'robot' && item.type === 'evaluate'
    })
    if (!evalForm) {
      // evalType: 1-机器人对话后定时销毁推送满意度 2-坐席邀评 3-访客主动评价 4-人工结束推送
      chat.updateMessages({
        role: 'system',
        type: 'evaluate',
        target: 'robot',
        evalType: 3,
        id: uuidv7(),
      })
    }

  }
  // 坐席评价
  const handleAgentEvaluation = () => {
    const session = useSessionStore()
    // 判断机器人评价是否已经完成
    if (session.agentEvaluate === evaluateState.complete) {
      showToast('您已评价,请勿重复评价！')
      return
    }
    // 判断当前是否已经有评价表单
    const evalForm = find(chat.messageList, (item:any) => {
      return item.target === 'agent' && item.type === 'evaluate'
    })
    if (!evalForm) {
      // evalType: 1-机器人对话后定时销毁推送满意度 2-坐席邀评 3-访客主动评价 4-人工结束推送
      chat.updateMessages({
        role: 'system',
        type: 'evaluate',
        target: 'agent',
        evalType: 3,
        id: uuidv7(),
      })
    }
  }
  // 结束会话
  const handleEndSession = () => {
    const session = useSessionStore()
    // 结束人工
    sendMessage({
      type: '@imCloud_subject_meeting_destroy@',
      msg: {
        loginName: session.visitorInformation.name,
        contentType: 'VisitorClose'
      }
    }, () => {
      // 主动结束
      session.updateActionState(FAction.DESTROY)
    })
  }
</script>

<template>
  <div
    v-if="visible"
    class="w-full grid grid-cols-5 grid-rows-1 gap-4 py-2 transition-all"
  >
    <div
      class="flex justify-center items-center flex-col"
      @click="openFileDialog"
    >
      <div
        class="w-[48px] h-[48px] mb-1  rounded-lg bg-white flex justify-center items-center"
      >
        <svg
          t="1736327036957"
          class="w-[28px] h-[28px]"
          viewBox="0 0 1024 1024"
          version="1.1"
          xmlns="http://www.w3.org/2000/svg"
          p-id="19099"
        >
          <path
            d="M919.04 416.46c0-17.868-5.12-22.988-22.989-22.988H130.304c-17.869 0-25.6 5.12-25.6 25.6v400.742c0 53.607 22.989 76.8 74.035 76.8h668.826c51.2 0 74.035-22.988 74.035-74.035-2.56-138.086-2.56-270.848-2.56-406.118z m-793.856-63.846H875.52c43.418 0 43.418 0 43.418-40.96 0-48.486-20.48-66.56-66.56-66.56H528.486c-40.96 0-40.96 0-40.96-40.96 0-43.417-20.48-61.44-63.795-61.44h-255.13c-46.08-2.56-63.795 15.36-66.56 61.44v125.543c0.359 20.429 5.479 22.937 23.143 22.937z"
            p-id="19100"
            fill="#666666"
          ></path>
        </svg>
      </div>
      <span class="text-[#666666] text-[14px]">文件</span>
    </div>
    <!-- 评价 -->
    <div v-if="showEvaluation" class="flex justify-center items-center flex-col" @click="sendEvaluation">
      <div
        class="w-[48px] h-[48px] mb-1 rounded-lg bg-white flex justify-center items-center"
      >
        <svg t="1740360495934" class="icon w-[28px] h-[28px]" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1534" width="200" height="200"><path d="M680.622785 0.003032H67.455882A40.462791 40.462791 0 0 0 26.501038 39.97377v645.322005a206.525013 206.525013 0 0 0 206.032959 205.975744h203.309502a34.890002 34.890002 0 0 0 24.568329-10.01271l412.99281-409.353904a34.581039 34.581039 0 0 0 10.550535-24.522557V206.024549A206.513569 206.513569 0 0 0 680.622785 0.003032zM254.436085 259.246392a35.908438 35.908438 0 0 1 5.870308 0H652.655856a34.832787 34.832787 0 0 1 0 69.413826H260.237735a34.832787 34.832787 0 1 1-5.80165-69.413826zM684.376121 477.523465a34.832787 34.832787 0 0 1-31.720265 31.651606H260.237735a34.832787 34.832787 0 1 1 0-69.413825H652.655856a34.84423 34.84423 0 0 1 31.720265 37.762219zM549.896846 869.483863a24.568329 24.568329 0 0 0-7.357911 13.479968l-18.88111 111.112471a26.776847 26.776847 0 0 0 5.412585 20.849322 26.010159 26.010159 0 0 0 19.373163 9.074376l6.1335-0.492053 106.947183-22.565787a24.488227 24.488227 0 0 0 12.507305-6.865858l254.208397-256.085065L804.849045 614.565994zM977.399503 688.476956a69.894436 69.894436 0 0 0 0-98.113112l-25.746968-24.522557a71.370595 71.370595 0 0 0-98.10167 0l-13.136675 13.136675L963.702116 702.265888z" p-id="1535" fill="#666666"></path></svg>
      </div>
      <span class="text-[#666666] text-[14px]">服务评价</span>
    </div>
<!--    结束会话-->
    <div v-if="showEndSession" class="flex justify-center items-center flex-col" @click="handleEndSession">
      <div
          class="w-[48px] h-[48px] mb-1 rounded-lg bg-white flex justify-center items-center"
      >
        <svg t="1740380950930" class="icon w-[28px] h-[28px]" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2565" width="200" height="200"><path d="M512.33 65.45c-246.32 0-446 199.68-446 446s199.68 446 446 446 446-199.68 446-446-199.68-446-446-446z m-37.98 166.83c0-10.69 3.69-19.9 11.08-27.7 7.35-7.8 16.24-11.71 26.59-11.71 10.76 0 19.86 3.91 27.21 11.71 7.39 7.8 11.08 17.01 11.08 27.7v233.98c0 10.67-3.69 19.81-11.08 27.42-7.35 7.57-16.45 11.38-27.21 11.38-10.35 0-19.23-3.81-26.59-11.38-7.39-7.61-11.08-16.75-11.08-27.42V232.28z m284.36 378.38c-14.15 33.46-33.31 62.6-57.42 87.43-24.11 24.83-52.44 44.52-84.91 59.12-32.48 14.53-67.25 21.84-104.36 21.84-36.66 0-71.26-7.3-103.73-21.84-32.49-14.6-60.91-34.28-85.21-59.12-24.32-24.84-43.47-53.97-57.42-87.47-13.96-33.42-20.92-69.21-20.92-107.42 0-21.78 2.49-43.12 7.47-64.04 4.98-20.96 12.06-40.84 21.24-59.73 9.16-18.9 20.52-36.54 34.08-52.95a292.655 292.655 0 0 1 45.45-44.32c8.76-6.58 18.24-9.05 28.4-7.4 10.18 1.64 18.43 6.76 24.79 15.39 6.41 8.64 8.82 18.27 7.21 28.95-1.6 10.67-6.59 19.29-14.96 25.86-25.11 18.87-44.34 42.06-57.7 69.56-13.36 27.53-20.03 57.09-20.03 88.68 0 27.11 4.98 52.66 14.95 76.64 9.97 23.98 23.62 44.95 40.96 62.84 17.35 17.82 37.67 32 60.98 42.44 23.35 10.48 48.15 15.75 74.46 15.75 26.31 0 51.15-5.26 74.46-15.75 23.31-10.45 43.66-24.63 60.98-42.45 17.35-17.89 31.11-38.87 41.26-62.84C698.92 555.85 704 530.3 704 503.19c0-32.03-7.18-62.3-21.53-90.83-14.35-28.52-34.46-52.03-60.38-70.5-8.78-6.15-14.04-14.57-15.85-25.25-1.78-10.66 0.31-20.52 6.27-29.56 5.99-8.61 14.15-13.85 24.53-15.69 10.38-1.84 19.92 0.31 28.7 6.46 17.94 13.15 33.9 28.02 47.84 44.64 13.94 16.62 25.93 34.59 35.89 53.9 9.97 19.27 17.53 39.69 22.72 61.25 5.19 21.56 7.77 43.4 7.77 65.58-0.03 38.22-7.1 74.01-21.25 107.47z" p-id="2566" fill="#666666"></path></svg>
      </div>
      <span class="text-[#666666] text-[14px]">结束会话</span>
    </div>
  </div>
</template>

<style scoped></style>
